
export const on = function (
  element: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject,
  useCapture = false
): void {
  if(element && event && handler) {
    element.addEventListener(event, handler, useCapture)
  }
}

export const off = function(
  element: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject
): void {
  if(element && event && handler) {
    element.removeEventListener(event, handler)
  }
}

export const once = function(
  element: HTMLElement,
  event: string,
  fn: EventListener
): void {
  const listener = function (...args: unknown[]) {
    if(fn) {
      // @ts-ignore
      fn.apply(this, args)
    }
    off(element, event, listener)
  }
  on(element, event, listener)
}
